package com.sywb.chuangyebao.contract;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.FindProjectBannerBean;
import com.sywb.chuangyebao.bean.FindProjectRecommendBean;
import com.sywb.chuangyebao.bean.GoToEvaluatingBean;
import com.sywb.chuangyebao.bean.ManuscriptManagenetInfo;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.bean.CategoryBean;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.AllCategoryActivity;
import com.sywb.chuangyebao.view.BusinessDetailsActivity;
import com.sywb.chuangyebao.view.WebActivity;
import com.sywb.chuangyebao.widget.GridSpacingItemDecoration;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemClickListener;
import org.bining.footstone.widget.PullToRefreshView;
import org.bining.footstone.widget.SlideView;

import java.util.ArrayList;
import java.util.List;

public interface FindProjectContract {

    interface FindProjectView extends BaseRefreshContract.View {

    }

    class Presenter extends BaseRefreshContract.Presenter<FindProjectView> {
        private SlideView banner;
        private BrandRecommendationAdapter brandRecommendationAdapter = null;
        private SortAdapter sortAdapter = null;
        private ManuscriptManagenetInfo sortInfo;
        private List<ManuscriptManagenetInfo> sortList;
        private int[] sortsImage = new int[]{R.drawable.f_food, R.drawable.f_closeth, R.drawable.f_furnishing, R.drawable.f_gift, R.drawable.f_decorate,
                R.drawable.f_protection, R.drawable.f_cosme, R.drawable.baby, R.drawable.f_education, R.drawable.f_all};

        private List<FindProjectBannerBean> mBannerList;//缓存数据库banner信息

        private List<Integer> sortID = null;

        private List<CategoryBean> categoryBeanList = new ArrayList<>();

        private View evaluatingView;

        private List<FindProjectRecommendBean> findProjectRecommendBeansList = new ArrayList<>();

        private PullToRefreshView commonRefresh;

        private RelativeLayout rlLayout;

        private String getEvaluatingAds = "";

        @Override
        public void onStart() {
            super.onStart();
            sortID = new ArrayList<>();
            sortID.add(2);
            sortID.add(1);
            sortID.add(6);
            sortID.add(5);
            sortID.add(8);
            sortID.add(9);
            sortID.add(4);
            sortID.add(7);
            sortID.add(11);
            sortID.add(100);
            sortAdapter = new SortAdapter(mContext);
            View headerView = mActivity.getLayoutInflater().inflate(R.layout.layout_find_project_head, null);
            rlLayout = headerView.findViewById(R.id.rl_layout);
            banner = headerView.findViewById(R.id.fAndP_banner);
            RecyclerView sortRecyclerView = headerView.findViewById(R.id.recycler_sort);
            evaluatingView = headerView.findViewById(R.id.evaluating_layout);
            initBanner();//加载banner图
            brandRecommendationAdapter = new BrandRecommendationAdapter(mContext);
            brandRecommendationAdapter.setHeaderView(headerView);
            setRecyclerAdapter(brandRecommendationAdapter);
            commonRefresh = mView.getPullToRefreshView();
            headerView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
            GridSpacingItemDecoration recyclerDivider = new GridSpacingItemDecoration(mActivity.getResources().getDimensionPixelSize(R.dimen.dp_15), mActivity.getResources().getDimensionPixelSize(R.dimen.dp_30), false);
            sortRecyclerView.addItemDecoration(recyclerDivider);
            sortRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 5));
            sortAdapter.addDatas(initSortDatas());
            sortRecyclerView.setAdapter(sortAdapter);
            commonRefresh.setVisibility(View.INVISIBLE);
            initData();
            goToEvaluating();
            evaluatingView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!getEvaluatingAds.equals("")) {
                        mView.advance(WebActivity.class, R.string.evaluating, getEvaluatingAds, "", false);
                    } else {
                        showMessage(R.string.no_data);
                    }
                }
            });
            sortAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
                    if (i == sortAdapter.getDataCount() - 1) {
                        mView.advance(AllCategoryActivity.class);
                    } else {
                        mView.advance(AllCategoryActivity.class, sortID.get(i) + "");
                    }
                }
            });
            getAllCategory();
        }

        @Override
        public void initData() {
            super.initData();
            recommendData();
        }

        @Override
        public void onStartRefresh() {
            super.onStartRefresh();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    recommendData();
                }
            }, 2000);
        }

        @Override
        public void onEndRefresh() {
            super.onEndRefresh();
            if (pullToRefreshView != null) {
                pullToRefreshView.refreshFinish(true);
            }
        }

        @Override
        public void onRecyclerItemClick(int postion) {
            super.onRecyclerItemClick(postion);
            mView.advance(BusinessDetailsActivity.class, findProjectRecommendBeansList.get(postion - 1).getProjectid());
        }

        private List<ManuscriptManagenetInfo> initSortDatas() {
            sortList = new ArrayList<>();
            String[] sortTitle = mContext.getResources().getStringArray(R.array.fp_sort_title);
            for (int i = 0; i < sortTitle.length - 2; i++) {
                sortInfo = new ManuscriptManagenetInfo(sortsImage[i], sortTitle[i], "");
                sortList.add(sortInfo);
            }
            sortList.add(new ManuscriptManagenetInfo(sortsImage[9], mContext.getResources().getString(R.string.allCategory), ""));
            return sortList;
        }

        /**
         * 全部分类数据
         */
        private void getAllCategory() {
            HttpUtils.getAllCategoryList(new HttpCallback<List<CategoryBean>>() {
                @Override
                public void onSuccess(List<CategoryBean> categoryBeanList) {
                    Presenter.this.categoryBeanList = categoryBeanList;
                }
            });
        }

        //服务分类
        class SortAdapter extends BaseRecyclerAdapter<ManuscriptManagenetInfo> {

            public SortAdapter(Context context) {
                super(context, R.layout.item_fp_sort);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, ManuscriptManagenetInfo sortInfo) {
                viewHolderHelper.setBackgroundRes(R.id.item_fp_sort_iv, sortInfo.getId());
                viewHolderHelper.setText(R.id.item_fp_sort_tv, sortInfo.getTitle());
            }
        }

        //品牌推荐
        class BrandRecommendationAdapter extends BaseRecyclerAdapter<FindProjectRecommendBean> {

            public BrandRecommendationAdapter(Context context) {
                super(context, R.layout.item_brand_recommend);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, final FindProjectRecommendBean info) {
                ImageView imageView = (ImageView) viewHolderHelper.getView(R.id.iv_img);
                TextView tvConsult = (TextView) viewHolderHelper.getView(R.id.tv_consult);
                tvConsult.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (info.getProjectid() != null) {
                            Utils.goConsult(Presenter.this, mView, Integer.parseInt(info.getProjectid())
                                    , 0, Constants.COOKIE_PROJECT);
                        }
                    }
                });
                if (info.getLogo() != null) {
                    GlideUtils.getNormalIamgeToUrl((Activity) mContext, info.getLogo(), imageView);
                }
                if (info.getTitle() != null && !info.getTitle().equals("")) {
                    viewHolderHelper.setText(R.id.tv_title, info.getProject_title());
                } else {
                    viewHolderHelper.setText(R.id.tv_title, R.string.no_data);
                }
                viewHolderHelper.setText(R.id.tv_industry_style, info.getCat_name());
                viewHolderHelper.setText(R.id.tv_amount_num, info.getInvestment());
                if (info.getCount() != null)
                    viewHolderHelper.setText(R.id.tv_views_num, info.getCount().getHits() + "");
            }
        }

        /**
         * banner接口接入
         */
        private void initBanner() {
            HttpUtils.findProjectBanner(new HttpCallback<List<FindProjectBannerBean>>() {
                @Override
                public void onSuccess(List<FindProjectBannerBean> findProjectBannerBeans) {
                    commonRefresh.setVisibility(View.VISIBLE);
                    mBannerList = new ArrayList<>();
                    for (int i = 0; i < findProjectBannerBeans.size(); i++) {
                        mBannerList.add(findProjectBannerBeans.get(i));
                    }
                    /**
                     * banner数据源
                     */
                    final List<String> images = new ArrayList<>();
                    if (mBannerList != null && mBannerList.size() > 0) {
                        for (int i = 0; i < mBannerList.size(); i++) {
                            images.add(mBannerList.get(i).getPhoto());
                        }
                        banner.setImageViewsToUrl(images, new SlideView.ImageLoader() {
                            @Override
                            public void displayImage(Context context, String s, ImageView imageView) {
                                GlideUtils.getNormalIamgeToUrl(mActivity, s, imageView, R.drawable.news_pic_default);
                            }
                        });
                        banner.setOnItemChildClickListener(new SlideView.OnItemChildClickListener() {
                            @Override
                            public void onItemChildClick(View view, int i) {
                                mView.advance(BusinessDetailsActivity.class, mBannerList.get(i)
                                        .getProject_id() + "");
                            }
                        });
                        banner.invalidate();
                    }
                }
            });
        }

        /**
         * 推荐数据
         */
        private void recommendData() {
            HttpUtils.findProjectRecommend(new HttpCallback<List<FindProjectRecommendBean>>() {
                @Override
                public void onStart() {
                    super.onStart();
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                    onFinishAsync();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }

                @Override
                public void onSuccess(List<FindProjectRecommendBean> findProjectRecommendBeans) {
                    commonRefresh.setVisibility(View.VISIBLE);
                    onEndRefresh();
                    findProjectRecommendBeansList = findProjectRecommendBeans;
                    brandRecommendationAdapter.setDatas(findProjectRecommendBeans);
                    rlLayout.setVisibility(View.VISIBLE);
                    //commonRefresh.setVisibility(View.VISIBLE);
                    onFinishAsync();
                }
            });
        }

        /**
         * 立即评测
         */
        private void goToEvaluating() {
            HttpUtils.goToEvaluating(new HttpCallback<GoToEvaluatingBean>() {
                @Override
                public void onSuccess(GoToEvaluatingBean goToEvaluatingBean) {
                    getEvaluatingAds = goToEvaluatingBean.getResult();
                }
            });
        }

    }
}
